﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Fluq
{
	sealed class SingletonLifestyle : IDependencyLifestyle
	{
		private int _count;
		private object _instance;

		public object Acquire(object graphId, IDependencyKey key, Func<object> factory)
		{
			if (++_count == 1)
			{
				_instance = factory();
			}

			return _instance;
		}

		public void Discard(IDependencyKey key, Action<object> disposer, object instance)
		{
			if (--_count == 0)
			{
				disposer(instance);

				_instance = null;
			}
		}
	}
}
